package com.tuanbiao.presenter;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.entity.BlastEntity;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.UploadDataController;
import com.tuanbiao.data.callback.OnBaseResultCallback;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/12 16:20
 */
public class UploadDataPresenter extends BasePresenter<UploadDataController.Model, UploadDataController.View> implements UploadDataController.Presenter {
    private int mPageIndex = 1;

    public UploadDataPresenter(UploadDataController.Model model, UploadDataController.View rootView) {
        super(model, rootView);
    }

    @Override
    public void refresh() {
        mPageIndex = 1;
        getUploadData(mPageIndex);
    }

    @Override
    public void loadMore() {
        mPageIndex++;
        getUploadData(mPageIndex);
    }

    @Override
    public void upload(int position,boolean isUploadAll, BlastEntity info) {
        Timber.i("upload#position:" + position + ",info:" + info);
        mRootView.showLoading();
        mModel.uploadBlast(info,isUploadAll, new OnBaseResultCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean aBoolean) {
                if (aBoolean) {
                    refresh();
                } else {
                    mRootView.showMessage("上传失败");
                }
            }

            @Override
            public void onFailed(String msg) {
                mRootView.showMessage(msg);
            }

            @Override
            public void onFinish() {
                mRootView.hideLoading();
            }
        });
    }

    @Override
    public void uploadAll() {
        mRootView.showMessage("嘿嘿，还没弄！");
        mModel.uploadBlastAll();
    }


    private void getUploadData(int pageIndex) {
        mModel.queryBlast(pageIndex, blastEntities -> {
            if (pageIndex == 1) mRootView.setItems(blastEntities);
            else mRootView.addItems(blastEntities);
        });
    }
}
